﻿<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output encoding="UTF-8" method="xml"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <oai_dc:dc xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/">
      <xsl:apply-templates select="Session/Formdata"/>
    </oai_dc:dc>
  </xsl:template>

  <xsl:template match="Session/Formdata">

<!-- Resource Type -->
    <xsl:choose>
      <xsl:when test="resource_type">
        <dc:type><xsl:apply-templates select="resource_type"/></dc:type>
        <xsl:variable name="rtype" select="resource_type"/>

<!-- NEED TO ADD CALL TO RESOURCE SPECIFIC TEMPLATES -->
        <xsl:if test="$rtype = 'Other Resource'">
          <xsl:call-template name="other_resource"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Moving Image'">
          <xsl:call-template name="moving_image"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Working Paper'">
          <xsl:call-template name="working_paper"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Journal Article'">
          <xsl:call-template name="journal_article"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Thesis'">
          <xsl:call-template name="thesis"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Report'">
          <xsl:call-template name="report"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Conference Paper'">
          <xsl:call-template name="conference_paper"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Conference Item'">
          <xsl:call-template name="conference_item"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Book Chapter'">
          <xsl:call-template name="book_chapter"/>
        </xsl:if>
        <xsl:if test="$rtype = 'Book'">
          <xsl:call-template name="book"/>
        </xsl:if>
      </xsl:when>
    </xsl:choose>

<!-- Language -->
    <xsl:choose>
      <xsl:when test="language">
        <dc:language>
          <xsl:apply-templates select="language"/>
        </dc:language>
      </xsl:when>
    </xsl:choose>

<!-- Generic Title -->
    <xsl:choose>
      <xsl:when test="title">
        <dc:title>
		<xsl:apply-templates select="title"/>
        </dc:title>
      </xsl:when>
    </xsl:choose>

<!-- Abstract -->
    <xsl:choose>
      <xsl:when test="description_abstract">
        <dc:description>
          <xsl:apply-templates select="description_abstract"/>
        </dc:description>
      </xsl:when>
    </xsl:choose>

<!-- Keywords -->
    <xsl:apply-templates select="*[starts-with(local-name(),'keyword')]"/>

<!-- Subjects -->
<!-- NEED TO SPLIT 'subject' by \r -->
    <xsl:choose>
      <xsl:when test="subject">TEST:1
        <xsl:call-template name="splitString">
          <xsl:with-param name="string1" select="subject"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>

    <xsl:choose>
      <xsl:when test="subject">TEST:2
        <xsl:call-template name="splitString">
          <xsl:with-param name="string1" select="subject"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>

<!-- Creators -->
    <xsl:choose>
      <xsl:when test="creators">
        <xsl:call-template name="splitStringNewline">
          <xsl:with-param name="stringLine" select="creators"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>

<!--Publisher Statement-->
    <xsl:choose>
      <xsl:when test="publisher_statement">
        <dc:relation>Publisher Statement:<xsl:apply-templates select="publisher_statement"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!--Aditional Info-->
    <xsl:choose>
      <xsl:when test="additional_info">
        <dc:relation>Additional Info:
	  <xsl:choose><xsl:when test="other_resource_info"><xsl:call-template name="splitStringInfo"><xsl:with-param name="stringLineInfo" select="other_resource_info"/></xsl:call-template><!--
	      <xsl:apply-templates select="other_resource_info"/>
--></xsl:when></xsl:choose>
	  <xsl:call-template name="splitStringInfo"><xsl:with-param name="stringLineInfo" select="additional_info"/></xsl:call-template>
<!--
	  <xsl:apply-templates select="additional_info"/>
-->
	</dc:relation>
      </xsl:when>
    </xsl:choose>


<!-- ============================== PREVIOUS CREATOR MAPPING ================================-->
<!-- Creators -->
<!--
    <dc:creator>
      <xsl:choose>
        <xsl:when test="creator_last_name">
	  <xsl:apply-templates select="creator_last_name"/>, <xsl:apply-templates select="creator_first_name"/>            
        </xsl:when>
      </xsl:choose>
    </dc:creator>
-->

<!-- Other Creators -->
<!--
    <xsl:apply-templates select="*[starts-with(local-name(),'creator_last_name_')]"/>
-->

<!-- Rights -->
<!--
    <xsl:choose>
      <xsl:when test="rights">
	<dc:rights>http://unsworks.unsw.edu.au/copyright</dc:rights>
        <dc:rights>The content is available under Creative Commons Attribution-NonCommercial-NoDerivs Licence</dc:rights>
	<dc:rights><xsl:apply-templates select="rights"/></dc:rights>
      </xsl:when>
    </xsl:choose>
-->
<dc:rights>http://unsworks.unsw.edu.au/copyright</dc:rights>
  </xsl:template>

<!-- ============================== END PREVIOUS CREATOR MAPPING ================================-->

<!-- ===================================Other Resource====================================== -->
  <xsl:template name="other_resource">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- ===================================Moving Image ====================================== -->
  <xsl:template name="moving_image">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Run Time -->
<xsl:choose>
        <xsl:when test="moving_image_hours">
                <xsl:choose>
                        <xsl:when test="moving_image_minutes">
                                <xsl:choose>
                                        <xsl:when test="moving_image_seconds">
                                                <dc:format>
       0<xsl:apply-templates select="moving_image_hours"/>:<xsl:apply-templates select="moving_image_minutes"/>:<xsl:apply-templates select="moving_image_seconds"/>
                                                </dc:format>
                                        </xsl:when>
                                        <xsl:otherwise>
                                                <dc:format>
       0<xsl:apply-templates select="moving_image_hours"/>:<xsl:apply-templates select="moving_image_minutes"/>:00
                                               </dc:format>
                                        </xsl:otherwise>
                                </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:when test="moving_image_seconds">
                                        <dc:format>
       0<xsl:apply-templates select="moving_image_hours"/>:00:<xsl:apply-templates select="moving_image_seconds"/>
                                        </dc:format>
                                </xsl:when>
                                <xsl:otherwise>
                                        <dc:format>
       0<xsl:apply-templates select="moving_image_hours"/>:00:00
                                       </dc:format>
                                </xsl:otherwise>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
                <xsl:choose>
                        <xsl:when test="moving_image_minutes">
                                <xsl:choose>
                                        <xsl:when test="moving_image_seconds">
                                                <dc:format>
       00:<xsl:apply-templates select="moving_image_minutes"/>:<xsl:apply-templates select="moving_image_seconds"/>
                                                </dc:format>
                                        </xsl:when>
                                        <xsl:otherwise>
                                                <dc:format>
       00:<xsl:apply-templates select="moving_image_minutes"/>:00
                                               </dc:format>
                                        </xsl:otherwise>
                                </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:when test="moving_image_seconds">
                                        <dc:format>
       00:00:<xsl:apply-templates select="moving_image_seconds"/>
                                        </dc:format>
                                </xsl:when>
                        </xsl:otherwise>
                </xsl:choose>
        </xsl:otherwise>
</xsl:choose>

<!-- Contributors -->
     <xsl:choose>
      <xsl:when test="moving_image_contributors">
        <xsl:call-template name="splitMICNewline">
           <xsl:with-param name="stringMICNewline" select="moving_image_contributors"/>
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>

  </xsl:template>

<!-- ===================================Report Template====================================== -->
  <xsl:template name="report">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Report Publisher -->
    <xsl:choose>
      <xsl:when test="report_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="report_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Report Place of Publication -->
    <xsl:choose>
      <xsl:when test="report_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="report_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Report ISBN -->
    <xsl:choose>
      <xsl:when test="report_isbn">
        <dc:identifier>ISBN:<xsl:apply-templates select="report_isbn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Report Edition -->
    <xsl:choose>
      <xsl:when test="report_edition">
        <dc:identifier>Edition:<xsl:apply-templates select="report_edition"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Report Series No -->
    <xsl:choose>
      <xsl:when test="report_series_no">
        <dc:identifier>Series No:<xsl:apply-templates select="report_series_no"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Report URL -->
    <xsl:choose>
      <xsl:when test="report_url">
        <dc:relation>URL:<xsl:apply-templates select="report_url"/></dc:relation>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- ===========================Working Paper Template=============================== -->
  <xsl:template name="working_paper">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- ISBN -->
    <xsl:choose>
      <xsl:when test="working_paper_isbn">
        <dc:identifier>ISBN:<xsl:apply-templates select="working_paper_isbn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- ISSN -->
    <xsl:choose>
      <xsl:when test="working_paper_issn">
        <dc:identifier>ISSN:<xsl:apply-templates select="working_paper_issn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Working Paper Series No -->
    <xsl:choose>
      <xsl:when test="working_paper_number">
        <dc:identifier>Series No:<xsl:apply-templates select="working_paper_number"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Working Paper Series Title -->
    <xsl:choose>
      <xsl:when test="working_paper_title">
        <dc:identifier>Series:<xsl:apply-templates select="working_paper_title"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Working Paper Publisher -->
    <xsl:choose>
      <xsl:when test="working_paper_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="working_paper_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Working Paper Place of Publication -->
    <xsl:choose>
      <xsl:when test="working_paper_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="working_paper_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Working Paper URL -->
    <xsl:choose>
      <xsl:when test="working_paper_url">
        <dc:relation>URL:<xsl:apply-templates select="working_paper_url"/></dc:relation>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- ================================Journal Article Template=========================== -->
  <xsl:template name="journal_article">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Journal Publisher -->
    <xsl:choose>
      <xsl:when test="journal_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="journal_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Journal Place of Publication -->
    <xsl:choose>
      <xsl:when test="journal_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="journal_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Journal Volume -->
    <xsl:choose>
      <xsl:when test="journal_volume">
        <dc:relation>Vol:<xsl:apply-templates select="journal_volume"/>
		<xsl:choose><xsl:when test="journal_issue">; Issue:<xsl:apply-templates select="journal_issue"/></xsl:when></xsl:choose>
		<xsl:choose><xsl:when test="journal_page_from">; pp.<xsl:apply-templates select="journal_page_from"/></xsl:when></xsl:choose>
		<xsl:choose><xsl:when test="journal_page_to">-<xsl:apply-templates select="journal_page_to"/></xsl:when></xsl:choose>
            </dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Journal Title -->
    <xsl:choose>
      <xsl:when test="journal_title">
        <dc:relation>Journal:<xsl:apply-templates select="journal_title"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Journal DOI URL-->
    <xsl:choose>
      <xsl:when test="journal_doi">
        <dc:relation>URL:<xsl:apply-templates select="journal_doi"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- ISSN -->
    <xsl:choose>
      <xsl:when test="journal_issn">
        <dc:identifier>ISSN:<xsl:apply-templates select="journal_issn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- =================================Thesis Template================================== -->
  <xsl:template name="thesis">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Supervisors -->
    <xsl:choose>
      <xsl:when test="thesis_supervisor_last_name">
        <xsl:choose>
          <xsl:when test="thesis_supervisor_first_name">
            <xsl:choose>
              <xsl:when test="thesis_supervisor_affiliation">
                <dc:contributor><xsl:apply-templates select="thesis_supervisor_last_name"/>, <xsl:apply-templates select="thesis_supervisor_first_name"/>, <xsl:apply-templates select="thesis_supervisor_affiliation"/></dc:contributor>
              </xsl:when>
              <xsl:otherwise>
                <dc:contributor><xsl:apply-templates select="thesis_supervisor_last_name"/>, <xsl:apply-templates select="thesis_supervisor_first_name"/></dc:contributor>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="thesis_supervisor_affiliation">
                <dc:contributor><xsl:apply-templates select="thesis_supervisor_last_name"/>, <xsl:apply-templates select="thesis_supervisor_first_name"/>, <xsl:apply-templates select="thesis_supervisor_affiliation"/></dc:contributor>
              </xsl:when>
              <xsl:otherwise>
                <dc:contributor><xsl:apply-templates select="thesis_supervisor_last_name"/>, <xsl:apply-templates select="thesis_supervisor_first_name"/></dc:contributor>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
    </xsl:choose>
<!--
	    <dc:contributor>
	        <xsl:apply-templates select="thesis_supervisor_affiliation"/>
            </dc:contributor>
-->

<!-- Other Supervisors -->
    <xsl:apply-templates select="*[starts-with(local-name(),'thesis_supervisor_last_name_')]"/>

<!-- Thesis Degree Program -->
    <xsl:choose>
      <xsl:when test="thesis_degree">
        <dc:type>
          <xsl:apply-templates select="thesis_degree"/>
        </dc:type>
      </xsl:when>
    </xsl:choose>

<!-- Thesis  -->
    <xsl:choose>
      <xsl:when test="thesis_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="thesis_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Thesis Place of Publication -->
    <xsl:choose>
      <xsl:when test="thesis_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="thesis_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- ============================Conference Paper Template========================== -->
  <xsl:template name="conference_paper">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>
    <xsl:choose>
      <xsl:when test="conference_month">
        <dc:date>
          <xsl:apply-templates select="conference_month"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Location of Conference -->
    <xsl:choose>
      <xsl:when test="conference_location">
        <dc:coverage>
          <xsl:apply-templates select="conference_location"/>
        </dc:coverage>
      </xsl:when>
    </xsl:choose>

<!-- NOTE: by changing the order of the xsl, we will need to change how the vital will show them as well in Library.vm -->
<!-- Conference Name/Title -->
    <xsl:choose>
      <xsl:when test="conference_name">
        <dc:relation>Conference:<xsl:apply-templates select="conference_name"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference URL -->
    <xsl:choose>
      <xsl:when test="conference_url">
        <dc:relation>URL:<xsl:apply-templates select="conference_url"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference Proceeding Page -->
    <xsl:choose>
      <xsl:when test="conference_page_from">
        <dc:relation>pp.<xsl:apply-templates select="conference_page_from"/>
	   <xsl:choose><xsl:when test="conference_page_to">-<xsl:apply-templates select="conference_page_to"/></xsl:when></xsl:choose>
	   </dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference Proceeding Year -->
    <xsl:choose>
      <xsl:when test="conference_proceedings_year">
        <dc:relation>Year:<xsl:apply-templates select="conference_proceedings_year"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference Proceeding Title -->
    <xsl:choose>
      <xsl:when test="conference_title">
        <dc:relation>Proceedings:<xsl:apply-templates select="conference_title"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- ISBN -->
    <xsl:choose>
      <xsl:when test="conference_isbn">
        <dc:identifier>ISBN:<xsl:apply-templates select="conference_isbn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>
<!-- ISSN -->
    <xsl:choose>
      <xsl:when test="conference_issn">
        <dc:identifier>ISSN:<xsl:apply-templates select="conference_issn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Conference Publisher -->
    <xsl:choose>
      <xsl:when test="conference_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="conference_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Conference Place of Publication -->
    <xsl:choose>
      <xsl:when test="conference_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="conference_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- ============================Conference Item Template========================== -->
  <xsl:template name="conference_item">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:choose>
            <xsl:when test="conference_item"><xsl:apply-templates select="conference_item_month"/> <xsl:apply-templates select="year"/></xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates select="year"/>
            </xsl:otherwise>
          </xsl:choose>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Location of Conference -->
    <xsl:choose>
      <xsl:when test="conference_item_location">
        <dc:coverage>
          <xsl:apply-templates select="conference_item_location"/>
        </dc:coverage>
      </xsl:when>
    </xsl:choose>

<!-- NOTE: by changing the order of the xsl, we will need to change how the vital will show them as well in Library.vm -->
<!-- Conference Name/Title -->
    <xsl:choose>
      <xsl:when test="conference_item_name">
        <dc:relation>Conference:<xsl:apply-templates select="conference_item_name"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference URL -->
    <xsl:choose>
      <xsl:when test="conference_item_url">
        <dc:relation>URL:<xsl:apply-templates select="conference_item_url"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference Proceeding Page -->
    <xsl:choose>
      <xsl:when test="conference_item_page_from">
        <dc:relation>pp.<xsl:apply-templates select="conference_item_page_from"/>
	   <xsl:choose><xsl:when test="conference_item_page_to">-<xsl:apply-templates select="conference_item_page_to"/></xsl:when></xsl:choose>
	   </dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference Proceeding Year -->
    <xsl:choose>
      <xsl:when test="conference_item_proceedings_year">
        <dc:relation>Year:<xsl:apply-templates select="conference_item_proceedings_year"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Conference Proceeding Title -->
    <xsl:choose>
      <xsl:when test="conference_item_title">
        <dc:relation>Proceedings:<xsl:apply-templates select="conference_item_title"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- ISBN -->
    <xsl:choose>
      <xsl:when test="conference_item_isbn">
        <dc:identifier>ISBN:<xsl:apply-templates select="conference_item_isbn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- ISSN -->
    <xsl:choose>
      <xsl:when test="conference_item_issn">
        <dc:identifier>ISSN:<xsl:apply-templates select="conference_item_issn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Conference Publisher -->
    <xsl:choose>
      <xsl:when test="conference_item_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="conference_item_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Conference Place of Publication -->
    <xsl:choose>
      <xsl:when test="conference_item_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="conference_item_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- =====================================Book Chapter Template================================= -->
  <xsl:template name="book_chapter">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Editors -->
    <xsl:choose>
      <xsl:when test="editor_last_name">
        <xsl:choose>
          <xsl:when test="editor_first_name">
            <dc:contributor><xsl:apply-templates select="editor_last_name"/>, <xsl:apply-templates select="editor_first_name"/></dc:contributor>
          </xsl:when>
          <xsl:otherwise>
            <dc:contributor>
              <xsl:apply-templates select="editor_last_name"/>
            </dc:contributor>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
    </xsl:choose>

<!-- Other Editors -->
    <xsl:apply-templates select="*[starts-with(local-name(),'editor_last_name_')]"/>

<!-- Book Chapter Edition -->
    <xsl:choose>
      <xsl:when test="chapter_page_from">
        <dc:relation>Edition:<xsl:apply-templates select="chapter_edition"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Book Chapter Page -->
    <xsl:choose>
      <xsl:when test="chapter_page_from">
        <xsl:choose>
          <xsl:when test="chapter_page_to">
            <dc:relation>pp.<xsl:apply-templates select="chapter_page_from"/>-<xsl:apply-templates select="chapter_page_to"/></dc:relation>
          </xsl:when>
          <xsl:otherwise>
            <dc:relation>pp.<xsl:apply-templates select="chapter_page_from"/></dc:relation>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
    </xsl:choose>

<!-- Book Chapter (Book Title) -->
    <xsl:choose>
      <xsl:when test="chapter_book_title">
        <dc:relation>Book:<xsl:apply-templates select="chapter_book_title"/></dc:relation>
      </xsl:when>
    </xsl:choose>

<!-- Book Chapter Publisher -->
    <xsl:choose>
      <xsl:when test="chapter_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="chapter_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Book Chapter Place of Publication -->
    <xsl:choose>
      <xsl:when test="chapter_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="chapter_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Book Chapter ISBN -->
    <xsl:choose>
      <xsl:when test="chapter_isbn">
        <dc:identifier>ISBN:<xsl:apply-templates select="chapter_isbn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Book Chapter URL -->
    <xsl:choose>
      <xsl:when test="chapter_url">
        <dc:relation>URL:<xsl:apply-templates select="chapter_url"/></dc:relation>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- ======================================Book Template====================================== -->
  <xsl:template name="book">

<!-- Date -->
    <xsl:choose>
      <xsl:when test="year">
        <dc:date>
          <xsl:apply-templates select="year"/>
        </dc:date>
      </xsl:when>
    </xsl:choose>

<!-- Book Publisher -->
    <xsl:choose>
      <xsl:when test="book_publisher">
        <dc:publisher>Publisher:<xsl:apply-templates select="book_publisher"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Book Place of Publication -->
    <xsl:choose>
      <xsl:when test="book_place_publication">
        <dc:publisher>Publication Place:<xsl:apply-templates select="book_place_publication"/></dc:publisher>
      </xsl:when>
    </xsl:choose>

<!-- Book ISBN -->
    <xsl:choose>
      <xsl:when test="book_isbn">
        <dc:identifier>ISBN:<xsl:apply-templates select="book_isbn"/></dc:identifier>
      </xsl:when>
    </xsl:choose>

<!-- Book URL -->
    <xsl:choose>
      <xsl:when test="book_url">
        <dc:relation>URL:<xsl:apply-templates select="book_url"/></dc:relation>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


<!-- ################# -->
<!-- Multiple Keywords -->
<!-- ################# -->
  <xsl:template match="*[starts-with(local-name(),'keyword')]">
    <dc:subject>
      <xsl:value-of select="."/>
    </dc:subject>
  </xsl:template>


<!-- ################# -->
<!-- Multiple Creators -->
<!-- ################# -->
  <xsl:template match="*[starts-with(local-name(),'creator_last_name_')]">
    <xsl:variable name="num" select="substring-after(name(),'creator_last_name_')"/>
    <xsl:variable name="first_name" select="concat('creator_first_name_',$num)"/>
    <dc:creator><xsl:value-of select="."/>, <xsl:value-of select="//*[local-name()=$first_name]"/></dc:creator>
  </xsl:template>


<!-- ################# -->
<!-- Multiple Supervisors -->
<!-- ################# -->
  <xsl:template match="*[starts-with(local-name(),'thesis_supervisor_last_name_')]">
    <xsl:variable name="num" select="substring-after(name(),'thesis_supervisor_last_name_')"/>
    <xsl:variable name="first_name" select="concat('thesis_supervisor_first_name_',$num)"/>
    <xsl:variable name="affiliation" select="concat('thesis_supervisor_affiliation_',$num)"/>
    <xsl:choose>
      <xsl:when test="//*[local-name()=$first_name]">
        <xsl:choose>
          <xsl:when test="//*[local-name()=$affiliation]">
            <dc:contributor><xsl:value-of select="."/>, <xsl:value-of select="//*[local-name()=$first_name]"/>, <xsl:value-of select="//*[local-name()=$affiliation]"/></dc:contributor>
          </xsl:when>
          <xsl:otherwise>
            <dc:contributor><xsl:value-of select="."/>, <xsl:value-of select="//*[local-name()=$first_name]"/></dc:contributor>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="//*[local-name()=$affiliation]">
            <dc:contributor><xsl:value-of select="."/>, <xsl:value-of select="//*[local-name()=$affiliation]"/></dc:contributor>
          </xsl:when>
          <xsl:otherwise>
            <dc:contributor>
              <xsl:value-of select="."/>
            </dc:contributor>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


<!-- ################# -->
<!-- Multiple Editors -->
<!-- ################# -->
  <xsl:template match="*[starts-with(local-name(),'editor_last_name_')]">
    <xsl:variable name="num" select="substring-after(name(),'editor_last_name_')"/>
    <xsl:variable name="first_name" select="concat('editor_first_name_',$num)"/>
    <xsl:choose>
      <xsl:when test="//*[local-name()=$first_name]">
        <dc:contributor><xsl:value-of select="."/>, <xsl:value-of select="//*[local-name()=$first_name]"/></dc:contributor>
      </xsl:when>
      <xsl:otherwise>
        <dc:contributor>
          <xsl:value-of select="."/>
        </dc:contributor>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


<!-- ################# -->
<!-- Split Creators function  -->
<!-- ################# -->
  <xsl:template name="splitStringNewline">
    <xsl:param name="stringLine"/>
    <xsl:choose>
      <xsl:when test="contains($stringLine,'&#13;')">
        <xsl:variable name="beforeCR" select="substring-before($stringLine,'&#13;')"/>
        <xsl:call-template name="splitStringCreator">
          <xsl:with-param name="stringCreator" select="$beforeCR"/>
        </xsl:call-template>
        <xsl:variable name="afterCR" select="substring-after($stringLine,'&#13;')"/>
        <xsl:call-template name="splitStringNewline">
          <xsl:with-param name="stringLine" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($stringLine,'&#10;')">
        <xsl:variable name="beforeCR" select="substring-before($stringLine,'&#10;')"/>
        <xsl:call-template name="splitStringCreator">
          <xsl:with-param name="stringCreator" select="$beforeCR"/>
        </xsl:call-template>
        <xsl:variable name="afterCR" select="substring-after($stringLine,'&#10;')"/>
        <xsl:call-template name="splitStringNewline">
          <xsl:with-param name="stringLine" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($stringLine,'&#xa;')">
        <xsl:variable name="beforeCR" select="substring-before($stringLine,'&#xa;')"/>
        <xsl:call-template name="splitStringCreator">
          <xsl:with-param name="stringCreator" select="$beforeCR"/>
        </xsl:call-template>
        <xsl:variable name="afterCR" select="substring-after($stringLine,'&#xa;')"/>
        <xsl:call-template name="splitStringNewline">
          <xsl:with-param name="stringLine" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="splitStringCreator">
          <xsl:with-param name="stringCreator" select="$stringLine"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="splitStringCreator">
    <xsl:param name="stringCreator"/>
    <xsl:choose>
      <xsl:when test="contains($stringCreator,';')">
        <xsl:variable name="beforeCR" select="substring-before($stringCreator,';')"/>
        <xsl:variable name="afterCR" select="substring-after($stringCreator,';')"/>
        <xsl:choose>
          <xsl:when test="$afterCR!=''">
            <dc:creator><xsl:value-of select="$beforeCR"/>,<xsl:value-of select="$afterCR"/></dc:creator>
          </xsl:when>
          <xsl:otherwise>
            <dc:creator>test <xsl:value-of select="$beforeCR"/></dc:creator>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$stringCreator!=''">
            <dc:creator>
              <xsl:value-of select="$stringCreator"/>
            </dc:creator>
          </xsl:when>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

<!-- ################################ -->
<!-- Split Moving Image Contributors  -->
<!-- ################################ -->
  <xsl:template name="splitMICNewline">
    <xsl:param name="stringMICNewline"/>
    <xsl:choose>
      <xsl:when test="contains($stringMICNewline,'&#13;')">
        <xsl:variable name="beforeCR" select="substring-before($stringMICNewline,'&#13;')"/>
        <xsl:call-template name="splitMIContributors">
          <xsl:with-param name="stringMIContributors" select="$beforeCR"/>
        </xsl:call-template>
        <xsl:variable name="afterCR" select="substring-after($stringMICNewline,'&#13;')"/>
        <xsl:call-template name="splitMICNewline">
          <xsl:with-param name="stringMICNewline" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($stringMICNewline,'&#10;')">
        <xsl:variable name="beforeCR" select="substring-before($stringMICNewline,'&#10;')"/>
        <xsl:call-template name="splitMIContributors">
          <xsl:with-param name="stringMIContributors" select="$beforeCR"/>
        </xsl:call-template>
        <xsl:variable name="afterCR" select="substring-after($stringMICNewline,'&#10;')"/>
        <xsl:call-template name="splitMICNewline">
          <xsl:with-param name="stringMICNewline" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($stringMICNewline,'&#xa;')">
        <xsl:variable name="beforeCR" select="substring-before($stringMICNewline,'&#xa;')"/>
        <xsl:call-template name="splitMIContributors">
          <xsl:with-param name="stringMIContributors" select="$beforeCR"/>
        </xsl:call-template>
        <xsl:variable name="afterCR" select="substring-after($stringMICNewline,'&#xa;')"/>
        <xsl:call-template name="splitMICNewline">
          <xsl:with-param name="stringMICNewline" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="splitMIContributors">
          <xsl:with-param name="stringMIContributors" select="$stringMICNewline"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="splitMIContributors">
    <xsl:param name="stringMIContributors"/>
    <xsl:choose>
      <xsl:when test="contains($stringMIContributors,': ')">
	<xsl:variable name="beforeCRT" select="substring-before($stringMIContributors,': ')"/>
	<xsl:variable name="afterCRT" select="substring-after($stringMIContributors,': ')"/>

    	<xsl:choose>
      	  <xsl:when test="contains($stringMIContributors,';')">
            <xsl:variable name="beforeCR" select="substring-before($afterCRT,'; ')"/>
            <xsl:variable name="afterCR" select="substring-after($afterCRT,'; ')"/>

    	    <xsl:choose>
	      <xsl:when test="$stringMIContributors!=''">

		<xsl:choose>
	  	  <xsl:when test="$beforeCRT!=''">

		    <xsl:choose>
	  	      <xsl:when test="$afterCR!=''">
		    	<dc:contributor><xsl:value-of select="$beforeCR"/>, <xsl:value-of select="$afterCR"/> (<xsl:value-of select="$beforeCRT"/>)</dc:contributor>
	              </xsl:when>
		      <xsl:otherwise>
			<dc:contributor><xsl:value-of select="$beforeCR"/> (<xsl:value-of select="$beforeCRT"/>)</dc:contributor>
		      </xsl:otherwise>
		    </xsl:choose>

	          </xsl:when>
		  <xsl:otherwise>

		    <xsl:choose>
	  	      <xsl:when test="$afterCR!=''">
		    	<dc:contributor><xsl:value-of select="$beforeCR"/>, <xsl:value-of select="$afterCR"/></dc:contributor>
	              </xsl:when>
		      <xsl:otherwise>
			<dc:contributor><xsl:value-of select="$beforeCR"/></dc:contributor>
		      </xsl:otherwise>
		    </xsl:choose>

		  </xsl:otherwise>
		</xsl:choose>

	      </xsl:when>
    	    </xsl:choose>
          
          </xsl:when>
	</xsl:choose>

      </xsl:when>
    </xsl:choose>

  </xsl:template>



<!--##########################-->
<!-- Multiple Subjects -->
<!--##########################-->
  <xsl:template name="splitString">
    <xsl:param name="string1"/>
    <xsl:choose>
      <xsl:when test="contains($string1,'&#13;')">
        <xsl:variable name="beforeCR" select="substring-before($string1,'&#13;')"/>
        <xsl:choose>
	   <xsl:when test="$beforeCR!=''">
              <xsl:element name="dc:subject">
          	<xsl:value-of select="$beforeCR"/>
              </xsl:element>
	   </xsl:when>
	</xsl:choose>
        <xsl:variable name="afterCR" select="substring-after($string1,'&#13;')"/>
        <xsl:call-template name="splitString">
          <xsl:with-param name="string1" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($string1,'&#10;')">
        <xsl:variable name="beforeCR" select="substring-before($string1,'&#10;')"/>
        <xsl:choose>
	   <xsl:when test="$beforeCR!=''">
              <xsl:element name="dc:subject">
          	<xsl:value-of select="$beforeCR"/>
              </xsl:element>
	   </xsl:when>
	</xsl:choose>
        <xsl:variable name="afterCR" select="substring-after($string1,'&#10;')"/>
        <xsl:call-template name="splitString">
          <xsl:with-param name="string1" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($string1,'&#xa;')">
        <xsl:variable name="beforeCR" select="substring-before($string1,'&#xa;')"/>
        <xsl:choose>
	   <xsl:when test="$beforeCR!=''">
              <xsl:element name="dc:subject">
          	<xsl:value-of select="$beforeCR"/>
              </xsl:element>
	   </xsl:when>
	</xsl:choose>
        <xsl:variable name="afterCR" select="substring-after($string1,'&#xa;')"/>
        <xsl:call-template name="splitString">
          <xsl:with-param name="string1" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$string1!=''">
            <xsl:element name="dc:subject">
              <xsl:value-of select="$string1"/>
            </xsl:element>
          </xsl:when>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


<!--##########################-->
<!-- Additional Info -->
<!--##########################-->
  <xsl:template name="splitStringInfo">
    <xsl:param name="stringLineInfo"/>
    <xsl:choose>
      <xsl:when test="contains($stringLineInfo,'&#13;')">
        <xsl:variable name="beforeCR" select="substring-before($stringLineInfo,'&#13;')"/>
        <xsl:variable name="afterCR" select="substring-after($stringLineInfo,'&#13;')"/>
        <xsl:value-of select="$beforeCR"/>
        <xsl:text>&#13;
</xsl:text>
        <xsl:call-template name="splitStringInfo">
          <xsl:with-param name="stringLineInfo" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($stringLineInfo,'&#10;')">
        <xsl:variable name="beforeCR" select="substring-before($stringLineInfo,'&#10;')"/>
        <xsl:variable name="afterCR" select="substring-after($stringLineInfo,'&#10;')"/>
        <xsl:value-of select="$beforeCR"/>
        <xsl:text>&#13;
</xsl:text>
        <xsl:call-template name="splitStringInfo">
          <xsl:with-param name="stringLineInfo" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="contains($stringLineInfo,'&#xa;')">
        <xsl:variable name="beforeCR" select="substring-before($stringLineInfo,'&#xa;')"/>
        <xsl:variable name="afterCR" select="substring-after($stringLineInfo,'&#xa;')"/>
        <xsl:value-of select="$beforeCR"/>
        <xsl:text>&#13;
</xsl:text>
        <xsl:call-template name="splitStringInfo">
          <xsl:with-param name="stringLineInfo" select="$afterCR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$stringLineInfo!=''">
            <xsl:value-of select="$stringLineInfo"/><xsl:text>&#13;
</xsl:text>
          </xsl:when>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
